package net.asfun.jvalog.resource;

import java.util.Collection;

import javax.jdo.Query;

import net.asfun.jvalog.common.Jdoer;
import net.asfun.jvalog.common.JdoerFactory;
import net.asfun.jvalog.entity.Label;
import net.asfun.jvalog.util.Unifier;

public class LabelDao {

	public static Collection<Label> listAll() {
		return JdoerFactory.jdoLabel.all();
	}

	public static Label getBySlogan(String slogan) {
		Jdoer<Label> jdl = JdoerFactory.jdoLabel;
		Query q = jdl.prepareQuery();
		q.setFilter("name == likes");
		q.declareParameters("String likes");
		return jdl.unique(q, slogan);
	}

	public static Label getOrSaveByName(String tag) {
		Jdoer<Label> jdl = JdoerFactory.jdoLabel;
		Query q = jdl.prepareQuery();
		q.setFilter("name == likes");
		q.declareParameters("String likes");
		Label label = jdl.unique(q, tag);
		if ( label == null ) {
			label = new Label();
			label.setName(tag);
			label.setSlogan(tag);
			jdl.save(label);
			Unifier.setUnique(label.getKey(), "slogan", tag);
		}
		return label;
	}
}
